package com.zp.crm.taskls.Timetask;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.zp.crm.service.PubService.PubService;

//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = "classpath:spring-mybatis.xml")
@Component
public class Timetask {
	
	@Resource
	public PubService pubService;
 
	@Scheduled(cron = "0 30 22 ? * *")
	//@Test
	 public void KhlsScheduled(){
		 System.out.println("定时方法开始：");
		
		 String time=Time();
		 
		 System.out.println("获取到7天前时间:"+time);
		 
		 String sql="select kh.code,kh.crmName as 'name',kh.crmstuts,MAX(dd.ddtime) as 'time',MAX(dd.ddtime)>'"+time+"' as 'mun',kh.crmusername as 'username' from crm_kh kh,crm_dd dd where kh.code=dd.code and crmstuts='正常' GROUP BY kh.code";

		 List<TakEntity> list=pubService.LsSelect(sql);
		 
		 System.out.println("数据库交互成功！");
		 
		 List<TakEntity> ulist=new ArrayList<>();
		 
		 System.out.println("查询到数据条数："+list.size());
		 
		 for (TakEntity e : list) {
			if(e.getMun()==0){
				ulist.add(e);
			}
		}
		 System.out.println("暂缓信息条数："+ulist.size());
		 if(ulist.size()!=0){
			 int update=pubService.LsUpdate(ulist,"暂缓流失");
			 int insert=pubService.LsInsert(ulist);
			 System.out.println("修改客户信息条数："+update);
			 System.out.println("增添流失条数："+insert);
		 }
	 }
	 
	 
	 
	 public static String Time(){
		 SimpleDateFormat fdata=new SimpleDateFormat("yyyy-MM-dd");
		 
		 Calendar c = Calendar.getInstance();
		 
		 c.setTime(new Date());
	     //	c.add(Calendar.MONTH, -6);过去6个月
		 c.add(Calendar.DATE, - 7);
	     
		 Date d = c.getTime();
	     
		 String day = fdata.format(d);
		
	     return day;
	 } 
}
